#include<iostream>
using namespace std;
const int N = 1e3 + 10;
int a[N], b[N];
int n;
int f[N][N];
int main()
{
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	for (int i = 1; i <= n; i++) {
		cin >> b[i];
	}
	for (int i = 1; i <= n; i++) {
		int ma = 1;
		for (int j = 1; j <= n; j++) {
			f[i][j] = f[i - 1][j];
			if (a[i] == b[j])f[i][j] = max(f[i][j], ma);
			if (a[i] > b[j])ma = max(ma, f[i - 1][j] + 1);
		}
	}
	int ans = -1e9;
	for (int i = 1; i <= n; i++) {
		ans = max(ans, f[n][i]);
	}
	cout << ans << endl;
	return 0;
}